<template>
  <div class="app-wrap">
    <h3 class="title">注册</h3>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="账号" prop="accountNumber">
        <el-input v-model.trim="ruleForm.accountNumber" autofocus></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model.trim="ruleForm.userName" ></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model.trim="ruleForm.nickName" ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="userPass">
        <el-input
          type="password"
          v-model.trim="ruleForm.userPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model.trim="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >注册</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-link class="register" @click="login" type="primary"
          >用户登录</el-link
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import request from "utils/request";
export default {
  name: "JddChromePluginsRegister",

  data() {
    var checkUserName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("不能为空"));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.userPass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        accountNumber:"",
        userPass: "",
        checkPass: "",
        userName: "",
        nickName: "",
      },
      rules: {
        accountNumber :[{ validator: checkUserName, trigger: "blur" }],
        userPass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        userName: [{ validator: checkUserName, trigger: "blur" }],
        nickName: [{ validator: checkUserName, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let ret = await request.post("/user/register", {
            accountNumber: this.ruleForm.accountNumber,
            userName: this.ruleForm.userName,
            nickName: this.ruleForm.nickName,
            passWord: this.ruleForm.userPass,
          });
          if (ret.code == 200) {
            this.$message({
              message: "注册成功",
              type: "success",
              showClose: true,
              duration:1000
            });
            this.$router.push({
              path: "/login",
            });
          } else {
            this.$message({
              message: ret.message,
              type: "error",
              showClose: true,
              duration:1000
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    login() {
      this.$router.push({
        path: "/login",
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.app-wrap {
  width: 550px;
  overflow: hidden;
  margin: 100px auto 0px;  .title {
    text-align: center;
    line-height: 50px;
    font-size: 30px;
    margin-bottom: 20px;
  }
  .register {
    margin-left: 20px;
  }
}
</style>